package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;
import com.wbdgj.adapter.ExampleAdapter;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.refresh.PullListView;
import com.wbdgj.utils.refresh.PullToRefreshLayout;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by Administrator on 2018/5/2.
 */

public class ExampleListActivity extends BaseActivity{

    @BindView(R.id.demo_listview)
    PullListView myPullListView;
    @BindView(R.id.pullToRefreshLayout)
    PullToRefreshLayout myPullToRefreshLayout;

    private ExampleAdapter exampleAdapter;

    private Context context = this;
    private int page = 1;
    private ArrayList<LinkedTreeMap> linkedTreeMapList;

    @Override
    protected int getLayoutResId() {
        return R.layout.demo_listview_refresh;
    }

    @Override
    protected void initView() {
        getData(page);

        myPullToRefreshLayout.setOnRefreshListener(new PullToRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
                pullToRefreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData(page);
                        myPullToRefreshLayout.refreshFinish(true);
                        ToastUtils.toastShort("刷新成功");
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
                page = page + 1;
                getData(page);
                myPullToRefreshLayout.loadMoreFinish(true);
            }
        });
    }

    private void getData(int page) {
        if (page == 1) {
            dutylist(page);
        } else {
            Adddutylist(page);
        }
    }

    private void dutylist(int page) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().dutylist(page+"", "10", "100116", BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
//                linkedTreeMapList = new ArrayList<>();
//                if(userModelBaseResultModel.getStatus()==0){
//                    linkedTreeMapList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
//                    exampleAdapter = new ExampleAdapter(context, linkedTreeMapList);
//                    myPullListView.setAdapter(exampleAdapter);
//                    if (linkedTreeMapList.size() < 1) {
//                        myPullListView.setPullUpEnable(false);
//                    }else{
//                        myPullListView.setPullUpEnable(true);
//                    }
//                    exampleAdapter.notifyDataSetChanged();
//                }else if(userModelBaseResultModel.getStatus()==1011){
//                    ToastUtils.toastShort(userModelBaseResultModel.getMsg());
//                    Intent intent = new Intent(context, LoginActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                    startActivity(intent);
//                }else{
//                    ToastUtils.toastShort(userModelBaseResultModel.getMsg());
//                }
            }
        });
    }

    private void Adddutylist(int page) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().dutylist(page+"","10", "100116", BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
//                if(userModelBaseResultModel.getStatus()==0){
//                    linkedTreeMapList = new ArrayList<>();
//                    linkedTreeMapList = (ArrayList<LinkedTreeMap>) userModelBaseResultModel.getData();
//                    exampleAdapter.loadMoreListView(linkedTreeMapList);
//                    exampleAdapter.notifyDataSetChanged();
//                }else if(userModelBaseResultModel.getStatus()==1011){
//                    ToastUtils.toastShort(userModelBaseResultModel.getMsg());
//                    Intent intent = new Intent(context, LoginActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//                    startActivity(intent);
//                }
            }
        });
    }
}
